package com.example.wto.viewpagertest01;

import android.database.Cursor;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wto on 4/15/2017.
 */

public class UtilTeacherSql implements IUtilSql
{
    private UtilSql utilSql = null;

    public UtilTeacherSql(UtilSqlHelper sqlHelper)
    {
        utilSql = new UtilSql(sqlHelper);
    }

    public void insertTeacher(Teacher teacher)
    {
        TeacherBean bean = teacher.getBean();
        String college_id = bean.getCollege_id();
        String college_name = bean.getCollge_name();
        String s_id = bean.getS_id();
        String s_name = bean.getS_name();
        String teacher_id = bean.getTeacherID();
        String teacher_name = bean.getTeacherName();

        String sql = "insert into teacher_table (college_id,college_name,s_id,s_name,teacher_id,teacher_name) values(?,?,?,?,?,?)";
        List<Object> params = new ArrayList<Object>();
        params.add(college_id);
        params.add(college_name);
        params.add(s_id);
        params.add(s_name);
        params.add(teacher_id);
        params.add(teacher_name);

        utilSql.update(sql, params);
        List<DayOfWeek> weekList = teacher.getWeekList();

        for (int i = 0; i < weekList.size(); i++)
        {
            params.clear();
            params.add(college_id);
            params.add(s_id);
            params.add(teacher_id);

            List<Course> courseList = weekList.get(i).getCourseList();
            String sql_one = "insert into week_teacher_table values(?,?,?,?,?,?,?,?,?,?,?)";
            params.add(weekList.get(i).getWeekName());
            params.add(i + "");
            for (int j = 0; j < courseList.size(); j++)
            {
                params.add(courseList.get(j).toString());
            }
            utilSql.update(sql_one, params);

        }
    }

    public Teacher selectTeacher(TeacherBean bean)
    {
        boolean flag = false;
        String sql = "select t.college_id,t.college_name,t.s_id,t.s_name,t.teacher_id,t.teacher_name,order_t,week_name,course_one,course_two,course_three,course_four,course_five,course_six from teacher_table t ,week_teacher_table w where t.college_id=w.college_id and t.s_id = w.s_id and t.teacher_id = w.teacher_id and t.college_id=? and t.s_id=? and t.teacher_id=? order by order_t;";
        Teacher teacher = new Teacher();
        Cursor resultSet = utilSql.select(sql, new String[]{bean.getCollege_id(), bean.getS_id(), bean.getTeacherID()});
        List<DayOfWeek> weekList = new ArrayList<DayOfWeek>();
        while (resultSet.moveToNext())
        {
            List<Course> courses = new ArrayList<Course>();
            DayOfWeek weekday = new DayOfWeek();
            bean.setCollge_name(resultSet.getString(resultSet.getColumnIndex("college_name")));
            bean.setS_name(resultSet.getString(resultSet.getColumnIndex("s_name")));
            bean.setTeacherName(resultSet.getString(resultSet.getColumnIndex("teacher_name")));
            //获取课程数据
            courses.add(new Course(resultSet.getString(resultSet.getColumnIndex("course_one"))));
            courses.add(new Course(resultSet.getString(resultSet.getColumnIndex("course_two"))));
            courses.add(new Course(resultSet.getString(resultSet.getColumnIndex("course_three"))));
            courses.add(new Course(resultSet.getString(resultSet.getColumnIndex("course_four"))));
            courses.add(new Course(resultSet.getString(resultSet.getColumnIndex("course_five"))));
            courses.add(new Course(resultSet.getString(resultSet.getColumnIndex("course_six"))));
            //设置星期几
            weekday.setWeekName(resultSet.getString(resultSet.getColumnIndex("week_name")));
            //添加课程数据
            weekday.getCourseList().clear();
            weekday.getCourseList().addAll(courses);
            //添加星期数据
            weekList.add(weekday);
            flag = true;
        }
        teacher.setBean(bean);
        resultSet.close();
        utilSql.close();
        if (flag)
        {
            teacher.getWeekList().clear();
            teacher.getWeekList().addAll(weekList);
            return teacher;
        } else
            return null;
    }

    @Override
    public void insert(Object obj)
    {
        Teacher teacher = (Teacher) obj;
        insertTeacher(teacher);
    }

    @Override
    public Object select(Object obj)
    {
        Teacher teacher = selectTeacher((TeacherBean) obj);
        return teacher;
    }
}